package org.example.practice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.ChoiceOptions;
import org.example.commonapi.domain.R;
import org.example.commonapi.util.options.ChoiceOptionsService;
import org.example.practice.until.PracticeUntil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/practice")
@Slf4j
public class ChoiceOptionsController {


    @Resource
    private ChoiceOptionsService choiceOptionsService;

    /**
     * 获取选项(前端进行选择处理)
     * @param practiceId
     * @return
     */
    @GetMapping("/choiceoptions/{practiceId}")
    public R getChoiceOptions(@PathVariable("practiceId") Long practiceId) {
        LambdaQueryWrapper<ChoiceOptions>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ChoiceOptions::getPracticeId,practiceId);
        ChoiceOptions choiceOptions=choiceOptionsService.getOne(queryWrapper);
        log.info("获取题目{}选项:{}",practiceId,choiceOptions);
        return R.success(choiceOptions);
    }


    /**
     * 新增题目,选择题
     * @param choiceOptions
     * @return
     */
    @PostMapping("/insert/choiceoptions/{type}")
    public R insertChoiceOptions(@PathVariable Integer type,@RequestBody ChoiceOptions choiceOptions) {
//        判断选项是否合法
        R result = PracticeUntil.isChoiceOptions(type,choiceOptions);
        if(result!=null)return result;
        choiceOptionsService.save(choiceOptions);
        return R.success();
    }

    /**
     * 更新题目,选择题
     * @param choiceOptions
     * @return
     */
    @PutMapping("/update/choiceoptions/{type}")
    public R updateChoiceOptions(@PathVariable Integer type,@RequestBody ChoiceOptions choiceOptions) {
//        判断选项是否合法
        R result = PracticeUntil.isChoiceOptions(type,choiceOptions);
        if(result!=null)return result;
        choiceOptionsService.updateById(choiceOptions);
        return R.success();
    }

    /**
     * 删除题目,选择题
     * @param practiceId
     * @return
     */
    @DeleteMapping("/delete/choiceoptions/{practiceId}")
    public R deleteChoiceOptions(@PathVariable("practiceId") Long practiceId) {
        LambdaQueryWrapper<ChoiceOptions>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ChoiceOptions::getPracticeId,practiceId);
        choiceOptionsService.remove(queryWrapper);
        return R.success();
    }

}
